package pa1.helloworld_example;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;

public class HelloServer extends UnicastRemoteObject
    implements Hello
{
    public HelloServer() throws RemoteException {
	System.out.println("Created HelloServer object.");
    }

    @Override
	public String sayHello() throws RemoteException {
	    return  ">>> Hello World, the current system time is " + new Date();
    }

    public static void main(String args[])
    {
        String port = Integer.toString(Registry.REGISTRY_PORT);
	if (args.length == 1) {
	    port = args[0];
	} else if (args.length > 1) {
            System.out.println("Usage: java helloworld.HelloServer [port]");
            System.exit(1);
        }
        try {
	    System.setSecurityManager( new RMISecurityManager() );
            HelloServer helloServer = new HelloServer();
            Naming.rebind(
                "//localhost:" + port + "/" + Hello.SERVICENAME,
                helloServer);
            System.out.println("Bound RMI object in local registry at port " + port);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
